package com.emmmya.ocs.modules.system.businessSetting.controller;

import com.emmmya.ocs.common.utils.ResultUtil;
import com.emmmya.ocs.common.vo.Result;
import com.emmmya.ocs.modules.system.businessSetting.entity.BusinessMailSetting;
import com.emmmya.ocs.modules.system.businessSetting.service.BusinessMailSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

/**
 * 创建人：咕噜肉IT小店-黑医
 * 创建时间 2022年09月23日 09:53:10
 * 描述：
 */
@RequestMapping("busnissSetting")
@Controller
@Slf4j
public class BusniessSettingController {

    @Autowired
    BusinessMailSettingService businessMailSettingService;


    @RequestMapping("setting/{key}")
    public String setting(@PathVariable("key") String key, Model model){
        if(!StringUtils.isEmpty(key)){
            BusinessMailSetting mailSetting = businessMailSettingService.findByKey(key);
            if(mailSetting != null){
                model.addAttribute("entity",mailSetting);
            }else{
                model.addAttribute("entity", new BusinessMailSetting(key));
            }

        }
        return "admin2/module/businessMailSetting/mail-setting";
    }

    @RequestMapping("saveMailSetting")
    @ResponseBody
    public Result<Object> saveMailSetting(@RequestBody BusinessMailSetting mailSetting){
        try {
            businessMailSettingService.save(mailSetting);
            return ResultUtil.success("保存成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.warn("【业务邮箱配置】保存数据时出现错误，错误信息："+e.getMessage());
            return ResultUtil.error(e);
        }
    }

}
